const fs = require("fs");
const path = require("path");
const startsWith = ['.', '_'];
const ignore = ['components'];
const home = ['index.tsx', 'index.jsx'];
const ext = ['.tsx', '.jsx'];
function getAllFile(dirPath) {
    const files = fs.readdirSync(dirPath);

    let arrayOfFiles = [];

    files.forEach(function (file) {
        const filePath = path.join(dirPath, file);
        const dirname = path.basename(filePath);
        const extname = path.extname(filePath)
        if (startsWith.some(name => dirname.startsWith(name))) {
            return
        }
        if (fs.statSync(filePath).isDirectory()) {
            if (ignore.includes(dirname)) {
                return
            }
            arrayOfFiles = [...arrayOfFiles, ...getAllFile(filePath)];
        } else {

            if (extname && !ext.includes(extname)) {

                return
            }
            arrayOfFiles.push(filePath);
        }
    });

    return arrayOfFiles;
}
function getEntryFile() {
    if(!fs.existsSync(path.resolve(process.cwd(), "./src/pages"))){
        return {}
    }
    const pages = {};
    const allFiles = getAllFile(path.resolve(process.cwd(), "./src/pages"));
    console.log({ allFiles });
    allFiles.forEach((key, index) => {
        const [, p] = key.split("src");
        console.log({ p }, key);
        const curDirPath = path.resolve(process.cwd(), "src");
        const curPath = key.replace(curDirPath, "");
        const basename = path.basename(curPath)
        const extname = path.extname(curPath)
        let entryName = '';
        if (home.includes(basename)) {
            entryName = path.dirname(curPath)
        } else {

            entryName = curPath.replace(extname, '')
        }
        pages[entryName] = key

    });
    console.log({ pages });

    return pages;
}
getEntryFile()
module.exports = getEntryFile